<?php

/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Sitepage
 * @copyright  Copyright 2010-2011 BigStep Technologies Pvt. Ltd.
 * @license    http://www.socialengineaddons.com/license/
 * @version    $Id: Controller.php 2011-05-05 9:40:21Z SocialEngineAddOns $
 * @author     SocialEngineAddOns
 */
class Sitepage_Widget_RecentlyPopularRandomSitepageController extends Engine_Content_Widget_Abstract {

  public function indexAction() {

    $showTabArray = Engine_Api::_()->getApi('settings', 'core')->getSetting('sitepage.ajax.widgets.list', array("0" => "1", "1" => "2", "2" => "3", "3" => "4", "4" => "5"));
    $ShowViewArray = Engine_Api::_()->getApi('settings', 'core')->getSetting('sitepage.ajax.widgets.layout', array("0" => "1", "1" => "2", "2" => "3"));
    $defaultOrder = Engine_Api::_()->getApi('settings', 'core')->getSetting('sitepage.ajax.layouts.oder', 1);
    $this->view->enablePrice = Engine_Api::_()->getApi('settings', 'core')->getSetting('sitepage.price.field', 1);
    $sitepage_most_viewed = Zend_Registry::isRegistered('sitepage_most_viewed') ? Zend_Registry::get('sitepage_most_viewed') : null;
    $this->view->list_view = 0;
    $this->view->grid_view = 0;
    $this->view->map_view = 0;
    $this->view->defaultView = -1;
    if (in_array("1", $ShowViewArray)) {
      $this->view->list_view = 1;
      if ($this->view->defaultView == -1 || $defaultOrder == 1)
        $this->view->defaultView = 0;
    }
    if (in_array("2", $ShowViewArray)) {
      $this->view->grid_view = 1;
      if ($this->view->defaultView == -1 || $defaultOrder == 2)
        $this->view->defaultView = 1;
    }
    if (in_array("3", $ShowViewArray)) {
      $this->view->map_view = 1;
      if ($this->view->defaultView == -1 || $defaultOrder == 3)
        $this->view->defaultView = 2;
    }
    if (empty($sitepage_most_viewed)) {
      return $this->setNoRender();
    }

    $sitepageRecently = array();
    $sitepageViewed = array();
    $sitepageRandom = array();
    $sitepageFeatured = array();
    $sitepageSponosred = array();

    if (in_array("1", $showTabArray)) {
      // GET SITEPAGE SITEPAGE FOR RECENTLY POSTED
      $sitepageRecently = Engine_Api::_()->sitepage()->getLising('Recently Posted', array('limit' => "1"));
    }
    if (in_array("2", $showTabArray)) {
      // GET SITEPAGE SITEPAGE FOR MOST VIEWES
      $sitepageViewed = Engine_Api::_()->sitepage()->getLising('Most Viewed', array('limit' => "1"));
    }
    if (in_array("3", $showTabArray)) {
      $sitepageRandom = Engine_Api::_()->sitepage()->getLising('Random', array('limit' => "1"));
    }

    if (in_array("4", $showTabArray)) {
      $sitepageFeatured = Engine_Api::_()->sitepage()->getLising('Featured', array('limit' => "1"));
    }

    if (in_array("5", $showTabArray)) {
      $sitepageSponosred = Engine_Api::_()->sitepage()->getLising('Sponosred', array('limit' => "1"));
    }

    if ((!(count($sitepageRecently) > 0) && !(count($sitepageViewed) > 0) && !(count($sitepageRandom) > 0 ) && !(count($sitepageFeatured) > 0 ) && !(count($sitepageSponosred) > 0 )) || ($this->view->defaultView == -1)) {
      return $this->setNoRender();
    }

    $this->view->active_tab1 = 0;
    $this->view->active_tab2 = 0;
    $this->view->active_tab3 = 0;
    $this->view->active_tab4 = 0;
    $this->view->active_tab5 = 0;
    $this->view->active_tab_list = 0;
    $this->view->active_tab_image = 0;

    if (count($sitepageRecently) > 0) {
      $this->view->sitepagesitepage = $sitepage = $sitepageRecently = Engine_Api::_()->sitepage()->getLising('Recently Posted');
      $this->view->active_tab1 = 1;
      $this->view->active_tab_list = (int) Engine_Api::_()->getApi('settings', 'core')->getSetting('sitepage.recent.widgets', 10);
      $this->view->active_tab_image = (int) Engine_Api::_()->getApi('settings', 'core')->getSetting('sitepage.recent.thumbs', 15);
    } else if (count($sitepageViewed) > 0) {
      $this->view->sitepagesitepage = $sitepage = $sitepageViewed = Engine_Api::_()->sitepage()->getLising('Most Viewed');
      $this->view->active_tab2 = 1;
      $this->view->active_tab_list = (int) Engine_Api::_()->getApi('settings', 'core')->getSetting('sitepage.popular.widgets', 10);
      $this->view->active_tab_image = (int) Engine_Api::_()->getApi('settings', 'core')->getSetting('sitepage.popular.thumbs', 15);
    } else if (count($sitepageRandom) > 0) {
      $this->view->sitepagesitepage = $sitepage = $sitepageRandom = Engine_Api::_()->sitepage()->getLising('Random');
      $this->view->active_tab3 = 1;
      $this->view->active_tab_list = (int) Engine_Api::_()->getApi('settings', 'core')->getSetting('sitepage.random.widgets', 10);
      $this->view->active_tab_image = (int) Engine_Api::_()->getApi('settings', 'core')->getSetting('sitepage.random.thumbs', 15);
    } else if (count($sitepageFeatured) > 0) {
      $this->view->sitepagesitepage = $sitepage = $sitepageFeatured = Engine_Api::_()->sitepage()->getLising('Featured');
      $this->view->active_tab4 = 1;
      $this->view->active_tab_list = (int) Engine_Api::_()->getApi('settings', 'core')->getSetting('sitepage.featured.list', 10);
      $this->view->active_tab_image = (int) Engine_Api::_()->getApi('settings', 'core')->getSetting('sitepage.featured.thumbs', 15);
    } else if (count($sitepageSponosred) > 0) {
      $this->view->sitepagesitepage = $sitepage = $sitepageSponosred = Engine_Api::_()->sitepage()->getLising('Sponosred');
      ;
      $this->view->active_tab5 = 1;
      $this->view->active_tab_list = (int) Engine_Api::_()->getApi('settings', 'core')->getSetting('sitepage.sponsored.list', 10);
      $this->view->active_tab_image = (int) Engine_Api::_()->getApi('settings', 'core')->getSetting('sitepage.sponsored.thumbs', 15);
    }

    if (count($sitepageRecently) > 0) {
      $this->view->tab1_show = 1;
    }
    if (count($sitepageViewed) > 0) {
      $this->view->tab2_show = 1;
    }
    if (count($sitepageRandom) > 0) {
      $this->view->tab3_show = 1;
    }

    if (count($sitepageFeatured) > 0) {
      $this->view->tab4_show = 1;
    }
    if (count($sitepageSponosred) > 0) {
      $this->view->tab5_show = 1;
    }

    $this->view->enableLocation = $checkLocation = Engine_Api::_()->sitepage()->enableLocation();
		$this->view->sitepage = '';

    if (!empty($this->view->map_view)) {

      $this->view->flageSponsored = 0;

      if (!empty($checkLocation)) {
        $ids = array();
        $sponsored = array();
        foreach ($sitepage as $sitepage_page) {
          $id = $sitepage_page->getIdentity();
          $ids[] = $id;
          $sitepage_temp[$id] = $sitepage_page;
        }
        $values['page_ids'] = $ids;

        $this->view->locations = $locations =  Engine_Api::_()->getDbtable('locations', 'sitepage')->getLocation($values);
        foreach ($locations as $location) {
          if ($sitepage_temp[$location->page_id]->sponsored) {
            $this->view->flageSponsored = 1;
            break;
          }
        }
        $this->view->sitepage = $sitepage_temp;
      }
    }

    //PAGE-RATING IS ENABLE OR NOT
    $this->view->ratngShow = (int) Engine_Api::_()->getDbtable('modules', 'core')->isModuleEnabled('sitepagereview');
  }

}
?>
